package com.itouchtv.yang.core.tools;

import cn.hutool.core.io.FileUtil;
import cn.hutool.core.text.csv.*;
import com.csvreader.CsvReader;

import java.io.IOException;
import java.nio.charset.Charset;
import java.util.*;

public class AnalysisCVS {

    /**
     * 按场景分类
     * @param path
     * @return
     */
    public static List<List<String[]>> readCVS(String path) {
        List<List<String[]>> groups = new ArrayList<>();
        List<String[]> group = null;

        Log.logger.info("开始加载测试用例..");
        try {
            // 创建CSV读对象
            CsvReader csvReader = new CsvReader(path, ',', Charset.forName("GBK"));
            // 读表头
            csvReader.readHeaders();
            while (csvReader.readRecord()) {
                String s[] = csvReader.getRawRecord().split("\t");
                if (Objects.equals(s[0],"")){
                    if (Objects.isNull(group)){
                        group = new ArrayList<>();
                    }
                    group.add(s);
                }else {
                    group = new ArrayList<>();
                    group.add(s);
                    groups.add(group);
                }
            }
        } catch (IOException e) {
            Log.logger.error("加载用例失败，没有找到:" + path);
            e.printStackTrace();
        }
        Log.logger.info("加载测试用例完成！");
        return groups;
    }

    public static List<Map<String,List<CsvRow>>> getDataDriverCsv(String path){
        Log.logger.info("开始加载测试用例:"+path);
        cn.hutool.core.text.csv.CsvReader reader = CsvUtil.getReader();
        reader.setContainsHeader(true);
        reader.setSkipEmptyRows(true);
        CsvData data = reader.read(FileUtil.file(path),Charset.forName("GBK"));
        List<CsvRow> rows = data.getRows();
        List<Map<String,List<CsvRow>>> scenarios= new ArrayList<>();
        Map<String,List<CsvRow>> scenario=new HashMap<>();
        List<CsvRow> csvRowList = new ArrayList<>();
        String id="";
        for (CsvRow csvRow:rows){
            String value = csvRow.getByName("用例编号");
            if (!Objects.equals(value,"") &&!Objects.equals(value,id)){
                if(!scenario.isEmpty()) scenario = new HashMap<>();
                if (!csvRowList.isEmpty()) csvRowList = new ArrayList<>();
                csvRowList.add(csvRow);
                scenario.put(value,csvRowList);
                id = value;
                scenarios.add(scenario);
            }else{
                csvRowList.add(csvRow);
            }
        }
        Log.logger.info("加载测试用例完成:"+path);
        return scenarios;
    }

    /**
     * 不需要按场景分类
     * @param path 文件路径
     * @return 返回LIST结果
     */
    public static List<List> readCVS2(String path) {
        List<List> groups = new ArrayList<>();
        List<String> group;

        Log.logger.info("开始加载测试用例..");
        try {
            // 创建CSV读对象
            CsvReader csvReader = new CsvReader(path, ',', Charset.forName("GBK"));
            // 读表头
            csvReader.readHeaders();
            while (csvReader.readRecord()) {
                String s[] = csvReader.getRawRecord().split("\t");
                group = new ArrayList<>();
                Collections.addAll(group, s);
                groups.add(group);
            }
        } catch (IOException e) {
            Log.logger.error("加载用例失败，没有找到:" + path);
            e.printStackTrace();
        }
        Log.logger.info("加载测试用例完成！");
        return groups;
    }
}
